﻿import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'non_gps_goal_setting_logic.dart';

/// 非GPS运动目标设置页面
class NonGpsGoalSettingView extends GetView<NonGpsGoalSettingLogic> {
  const NonGpsGoalSettingView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.black,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
          onPressed: () => Get.back(),
        ),
        title: const Text(
          '目标设置',
          style: TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.w600,
          ),
        ),
        centerTitle: true,
      ),
      body: SafeArea(
        child: Column(
          children: [
            SizedBox(height: 20.h),
            _buildTabBar(),
            SizedBox(height: 40.h),
            _buildCurrentValue(),
            SizedBox(height: 40.h),
            _buildPresetOptions(),
            const Spacer(),
            _buildConfirmButton(),
            SizedBox(height: 20.h),
          ],
        ),
      ),
    );
  }

  /// 构建标签栏
  Widget _buildTabBar() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 20.w),
      child: Obx(() => Row(
        children: [
          _buildTab('时间', 0),
          _buildTab('卡路里', 1),
        ],
      )),
    );
  }

  /// 构建单个标签
  Widget _buildTab(String title, int index) {
    final isSelected = controller.selectedTabIndex.value == index;
    
    return Expanded(
      child: GestureDetector(
        onTap: () => controller.selectTab(index),
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 12.h),
          margin: EdgeInsets.symmetric(horizontal: 4.w),
          decoration: BoxDecoration(
            color: isSelected ? Colors.orange : Colors.transparent,
            borderRadius: BorderRadius.circular(20.r),
          ),
          child: Center(
            child: Text(
              title,
              style: TextStyle(
                color: isSelected ? Colors.black : Colors.white,
                fontSize: 14.sp,
                fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
              ),
            ),
          ),
        ),
      ),
    );
  }

  /// 构建当前值显示
  Widget _buildCurrentValue() {
    return Obx(() {
      final currentValue = controller.getCurrentValue();
      final unit = controller.getCurrentUnit();
      
      return Column(
        children: [
          Text(
            currentValue,
            style: TextStyle(
              color: Colors.white,
              fontSize: 72.sp,
              fontWeight: FontWeight.w300,
            ),
          ),
          SizedBox(height: 8.h),
          Text(
            unit,
            style: TextStyle(
              color: Colors.grey[400],
              fontSize: 18.sp,
            ),
          ),
        ],
      );
    });
  }

  /// 构建预设选项
  Widget _buildPresetOptions() {
    return Expanded(
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 20.w),
        child: Obx(() {
          final options = controller.getPresetOptions();
          
          return GridView.count(
            physics: const NeverScrollableScrollPhysics(),
            crossAxisCount: 3,
            childAspectRatio: 2.2,
            crossAxisSpacing: 12,
            mainAxisSpacing: 12,
            children: options.map((option) {
              return Obx(() {
                final isSelected = controller.isOptionSelected(option);
                
                return GestureDetector(
                  onTap: () => controller.selectOption(option),
                  child: Container(
                    decoration: BoxDecoration(
                      color: isSelected ? Colors.orange : Colors.grey[800],
                      borderRadius: BorderRadius.circular(12.r),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          option.value,
                          style: TextStyle(
                            color: isSelected ? Colors.black : Colors.white,
                            fontSize: 16.sp,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        if (option.description.isNotEmpty) 
                          Container(
                            margin: EdgeInsets.only(top: 2.h),
                            child: Text(
                              option.description,
                              style: TextStyle(
                                color: isSelected ? Colors.black.withOpacity(0.7) : Colors.grey[400],
                                fontSize: 10.sp,
                              ),
                            ),
                          ),
                      ],
                    ),
                  ),
                );
              });
            }).toList(),
          );
        }),
      ),
    );
  }

  /// 构建确认按钮
  Widget _buildConfirmButton() {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.symmetric(horizontal: 20.w),
      child: ElevatedButton(
        onPressed: () => controller.confirmSetting(),
        style: ElevatedButton.styleFrom(
          backgroundColor: Colors.orange,
          foregroundColor: Colors.black,
          padding: EdgeInsets.symmetric(vertical: 16.h),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(25.r),
          ),
          elevation: 0,
        ),
        child: Text(
          '进入',
          style: TextStyle(
            fontSize: 18.sp,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),
    );
  }
}
